var app = getApp();
var util = require("../../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    time: util.formatTime(new Date()).substring(0, 19).replace(/\//g, "-"),
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    wx.showLoading({
      title: '加载中…',
    })
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    this.setData({
      token: extConfig.appid
    })
    console.log(that.data.time);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.request({
      url: 'https://www.58zltc.com/api.php/index/my_message?openid=' + app.globalData.openid +'&token=' + this.data.token,
      success: res => {
        this.setData({
          list: res.data.list
        })
        wx.hideLoading();
      }
    })
  },
  top: function (e) {
    var id = e.currentTarget.dataset.id;
    var action = e.currentTarget.dataset.action;
    wx.navigateTo({
      url: 'top?id=' + id +"&action=" + action,
    })
  },
  refresh: function (e) {
    var id = e.currentTarget.dataset.id;
    var action = e.currentTarget.dataset.action;
    wx.request({
      url: 'https://www.58zltc.com/api.php/Info/refresh?id=' + id + '&action=' + action + '&token=' + this.data.token,
      success: res => {
        if (res.data.err_code == 400){
          wx.showModal({
            title: '提醒',
            content: res.data.err_msg,
            showCancel: false
          })
        }else{
          wx.showToast({
            title: res.data.err_msg,
          })
        }
      }
    })
  },
  edit: function (e) {
    var id = e.currentTarget.dataset.id;
    var action = e.currentTarget.dataset.action;
    var cid = e.currentTarget.dataset.cid;
    wx.navigateTo({
      url: 'add?id=' + id + "&action=" + action +"&cid=" +cid,
    })
  },
  del: function (e) {
    wx.showModal({
      title: '提醒',
      content: "确定要删除本信息，删除后无法恢复？",
      success: res=>{
        if (res.cancel) {
          
        } else {
          var id = e.currentTarget.dataset.id;
          var action = e.currentTarget.dataset.action;
          wx.request({
            url: 'https://www.58zltc.com/api.php/Info/del?id=' + id + '&action=' + action + '&token=' + this.data.token,
            success: res => {
              if (res.data.err_code == 400) {
                wx.showModal({
                  title: '提醒',
                  content: res.data.err_msg,
                  showCancel: false
                })
              } else {
                wx.showToast({
                  title: res.data.err_msg,
                })
              }
              this.onShow();
            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})